package com.lagou.homework.servlet;

import com.lagou.homework.service.StudentManageService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 实现删除学生信息的Servlet
 */
@WebServlet(name = "DelServlet", urlPatterns = "/delServlet")
public class DelServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 0.设置编码方式
        request.setCharacterEncoding("utf-8");

        // 1.创建StudentManageService对象
        StudentManageService studentManageService = new StudentManageService();
        // 2.从request中获取要删除的学生的学号
        int code = Integer.parseInt(request.getParameter("del").trim());
        // 3.执行删除操作
        int i = studentManageService.delStudentService(code);
        if (1 == i) {
            System.out.println("删除学生信息成功！");
        } else {
            System.out.println("删除学生信息失败！");
        }
        // 4.浏览器跳转，回到主页面
        response.sendRedirect("main.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
